﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using Lucene.Net.Documents;
using ZLCMS.Web.Code;
using System.Collections;

namespace ZLCMS.Web.Example.Lucene
{
    public partial class WriteIndex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public  void CreateIndex(string ttl, string ctnt)
        {
            Document doc = new Document();
            doc.Add(new Field("title", ttl, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("content", ctnt, Field.Store.YES, Field.Index.ANALYZED));
            LuceneUtil.Instance.Write(new List<Document>() { doc });
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
// 
//             CreateIndex("jeffreyzhao", "博客园有一个老赵，人格魅力巨大，洋名就叫jeffreyzhao。据我所知，他还是一个胖子，一个钢琴业余爱好者。");
//             CreateIndex("lucene测试", "这是一个测试，关于lucene.net的 关注老赵");
//             CreateIndex("博客园里有牛人", "Hello World. 我认识的一个高手，他拥有广博的知识，有极客的态度，还经常到园子里来看看");
//             CreateIndex("奥巴马", "美国现任总统是奥巴马？确定不是奥巴牛和奥巴羊 不知道问老赵");
//             CreateIndex("奥林匹克", "奥林匹克运动会将来到南美美丽热情的国度巴西，也就是亚马逊河流域的一个地方");
//             CreateIndex("写给自己", "博客园的jeffwong，新的开始，继续努力了");

            Document doc = new Document();
            doc.Add(new Field("title", TextBox1.Text, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("content", TextBox2.Text, Field.Store.YES, Field.Index.ANALYZED));
            LuceneUtil.Instance.IndexDirectory = Server.MapPath("~/index");
            LuceneUtil.Instance.Write(new List<Document>() { doc });
            ScriptManager.RegisterClientScriptBlock(this, typeof(WriteIndex), "script", "alert('已写入索引')", true);
            //TextBox1.Text = "";
            //TextBox2.Text = "";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            LuceneUtil.Instance.IndexDirectory = Server.MapPath("~/index");
            LuceneUtil.Instance.ClearIndexs();

            ExcelLoader loader = new ExcelLoader();
            loader.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/App_Data/data.xlsx") + ";Extended Properties=Excel 12.0 Xml;";
            IList<IDictionary> list = loader.LoadSource("");

            List < Document > docs = new List<Document>();
            foreach (IDictionary dic in list)
            {
                Document doc = new Document();

                doc.Add(new Field("name", dic["名称"] as string, Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("code", dic["编号"] as string, Field.Store.YES, Field.Index.NO));
                doc.Add(new Field("py", dic["拼音"] as string, Field.Store.YES, Field.Index.ANALYZED));

                docs.Add(doc);
            }

            LuceneUtil.Instance.Write(docs);
            ScriptManager.RegisterClientScriptBlock(this, typeof(WriteIndex), "script", "alert('已写" + list.Count + "条记录')", true);
        }
    }
}
